<?php

use Phalcon\Paginator\Adapter\Model as PaginatorModel;
use Phalcon\Paginator\Adapter\NativeArray as PaginatorArray;


/**
 * 巨鱼商贸商品
 */
class BGoodsController extends ControllerBase
{
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 巨鱼商贸商品列表
     */
    public function listAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $goods = new BGoods();
        $count = $goods->goods_count($this->params);
        $list  = [];
        if ($count) {
            $list = $goods->goods_list($this->params)->toarray();
        }
        $result = ToolsController::paging($list, $count, $this->params['limit'], $this->params['page']);

        $this->ok($result);
    }

    /**
     * 巨鱼商贸商品列表
     */
    public function addAction()
    {
        $data   = $this->goods_validate_field($this->params);
        $model  = new BGoods();
        $result = $model->goods_save($data);
//        if ($result === false) {
//            $messages = $model->getMessages();
//            foreach ($messages as $message) {
//                echo $message, "\n";
//            }
//            exit;
//        }
        if ($result) {
            $goods = $model->findFirst('id=' . $model->id);
            if (!$goods->item_id) {
                $goods->item_id = 10000 + $model->id;
                $goods->update();
            }

            $this->ok([]);
        } else {
            $this->fail(-1, '添加失败');
        }

    }

    /**
     * 巨鱼商贸商品置顶
     */
    public function updateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $data = [];
        $type = isset($this->params['type']) ? intval($this->params['type']) : 0;
        if (empty($type)) {//编辑
            $data = $this->goods_validate_field($this->params);
        } else {
            $data['status']     = isset($this->params['status']) ? intval($this->params['status']) : 0;
            $data['updated_at'] = date('Y-m-d H:i:s', time());
        }
        $id = trim($this->params['id']);
//        $ids   = explode(',', $id);
        $goods  = new BGoods();
        $result = $goods->find(['id In (' . $id . ')'])->update($data);

        if ($result === false) {
            $messages = $goods->getMessages();
            foreach ($messages as $message) {
                echo $message, "\n";
            }
            exit;
        }

        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }
    }

    private function goods_validate_field($params)
    {
        if (!isset($params['title']) || empty($params['title'])) {
            $this->fail(-1, '商品标题不能为空');
        }
        if (!isset($params['price']) || empty($params['price'])) {
            $this->fail(-1, '商品价格不能为空');
        }
        if (!isset($params['pic_logo']) || empty($params['pic_logo'])) {
            $this->fail(-1, '商品图片不能为空');
        }

        if (!isset($params['stock']) || empty($params['stock'])) {
            $this->fail(-1, '商品库存不能为空');
        }
        if ((empty($params['category_id']) && empty($params['category_second_id'])) && (empty($params['column_id']) && empty($params['column_second_id']))) {
            $this->fail(-1, '商品分类/栏目不能都为空');
        }
        $data['title']              = trim($params['title']);
        $data['pic_logo']           = trim($params['pic_logo']);
        $data['contribution']       = trim($params['contribution']);
        $data['deduct']             = trim($params['deduct']);
        $data['title']              = trim($params['title']);
        $data['size']               = trim($params['size']);
        $data['category_id']        = isset($params['category_id']) ? intval($params['category_id']) : 0;
        $data['category_second_id'] = isset($params['category_second_id']) ? intval($params['category_second_id']) : 0;
        $data['column_id']          = isset($params['column_id']) ? intval($params['column_id']) : 0;
        $data['column_second_id']   = isset($params['column_second_id']) ? intval($params['column_second_id']) : 0;
        $data['item_id']            = isset($params['item_id']) ? intval($params['item_id']) : 0;
        $data['sort']               = isset($params['sort']) ? intval($params['sort']) : 0;
        $data['grade']              = isset($params['grade']) ? intval($params['grade']) : 0;
        $data['upgrade']            = isset($params['upgrade']) ? intval($params['upgrade']) : 0;
        $data['detail']             = trim($params['detail']);
        $data['price']              = intval($params['price']);
        $data['stock']              = intval($params['stock']);
        if (isset($params['id']) && $params['id']) {
            $data['updated_at'] = date('Y-m-d H:i:s', time());
        } else {
            $data['created_at'] = date('Y-m-d H:i:s', time());
        }
        return $data;
    }

    /**
     * 删除巨鱼商贸商品
     */
    public function deleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $id     = trim($this->params['id']);
        $goods  = new BGoods();
        $result = $goods->find(['id In (' . $id . ')'])->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '删除失败');
        }

    }

    /**
     * 巨鱼商贸商品详情
     */
    public function detailsAction()
    {
        if (!isset($this->params['item_id']) || empty($this->params['item_id'])) {
            $this->fail(-1, 'item_id不能为空');
        }
        $id     = intval($this->params['item_id']);
        $goods  = new BGoods();
        $result = $goods->findFirst(['item_id =' . $id]);

        if ($result) {
            $this->ok($result->toarray());
        } else {
            $this->fail(-1, '没有该商品');
        }

    }

    /**
     * 巨鱼商贸商品栏目和分类
     */
    public function columncateAction()
    {
        $model              = new BGoodsColumn();
        $categories         = new BGoodsCategories();
        $tree               = new Tree();
        $list               = $model->goods_column_all('id as value,name as label,parent_id')->toarray();
        $res                = $categories->goods_categories_all('id as value,name as label,parent_id')->toarray();
        $data['column']     = $tree->list2tree($list, 'value', 'parent_id', 'children');
        $data['categories'] = $tree->list2tree($res, 'value', 'parent_id', 'children');

        $this->ok($data);
    }

    /**
     * 添加栏目
     */
    public function caddAction()
    {
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '栏目分类名称不能为空');
        }
        if (!isset($this->params['parent_id'])) {
            $this->fail(-1, '父级不能为空');
        }
        $info['name']       = trim($this->params['name']);
        $info['parent_id']  = intval($this->params['parent_id']);
        $info['created_at'] = date('Y-m-d H:i:s');
        $model              = new BGoodsColumn();
        $result             = $model->save_cloumn($info);
        if ($result) {
            if (!$info['parent_id']) {
                $goods        = $model->findFirst('id=' . $model->id);
                $goods->value = $model->id;
                $goods->update();
            }
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 获取栏目父级
     */
    public function cparentAction()
    {

        $model  = new BGoodsColumn();
        $result = $model->goods_column_parents();
        if ($result) {
            $this->ok($result);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 修改栏目
     */
    public function cupdateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '栏目分类id不能为空');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '栏目分类名称不能为空');
        }
        if (!isset($this->params['parent_id'])) {
            $this->fail(-1, '父级不能为空');
        }
        $collegeColumn = new BGoodsColumn();
        $info          = $collegeColumn->find_by_id($this->params['id']);
        if (empty($info)) {
            $this->fail(-1, '该记录不存在，或已被删除');
        }

        $info['name']       = trim($this->params['name']);
        $info['parent_id']  = intval($this->params['parent_id']);
        $info['updated_at'] = date('Y-m-d H:i:s');
        $model              = new BGoodsColumn();
        $result             = $model->save_cloumn($info);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 删除栏目
     */
    public function cdeleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '栏目id不能为空');
        }
        $model = new BGoodsColumn();
        $info  = $model->find_by_id($this->params['id']);
        if (empty($info)) {
            $this->fail(-1, '该记录不存在，或已被删除');
        }
        if (empty($info['parent_id'])) {// 顶级
            $child = $model->find_by_parent_id($info['id']);
            if ($child) {
                $this->fail(-1, '该栏目下面有子栏目，不能删除');
            }
        } else { // 二级
//            $count = (new Goods())->goods_count_by(['column_second_id =' . $info['value']]);
//            if ($count) {
//                $this->fail(-1, '该栏目下面有商品，不能删除');
//            }
        }
        $result = $model->findFirstById($this->params['id'])->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 巨鱼商贸商品商品栏目列表
     */
    public function clistAction()
    {
        $model  = new BGoodsColumn();
        $tree   = new Tree();
        $list   = $model->goods_column_all()->toarray();
        $mytree = $tree->array2tree($list, 'name', 'id', 'parent_id');
        $this->ok($mytree);

    }


    /**
     * 添加分类
     */
    public function caaddAction()
    {
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '栏目分类名称不能为空');
        }
        if (!isset($this->params['parent_id'])) {
            $this->fail(-1, '父级不能为空');
        }
        $info['name']       = trim($this->params['name']);
        $info['parent_id']  = intval($this->params['parent_id']);
        $info['image']      = trim($this->params['image']);
        $info['created_at'] = date('Y-m-d H:i:s');
        $model              = new BGoodsCategories();
        $result             = $model->save_categories($info);
        if ($result) {
            if (!$info['parent_id']) {
                $goods        = $model->findFirst('id=' . $model->id);
                $goods->value = $model->id;
                $goods->update();
            }
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 获取分类父级
     */
    public function caparentAction()
    {

        $model  = new BGoodsCategories();
        $result = $model->goods_categories_parents();
        if ($result) {
            $this->ok($result);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 修改分类
     */
    public function caupdateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '栏目分类id不能为空');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '栏目分类名称不能为空');
        }
        if (!isset($this->params['parent_id'])) {
            $this->fail(-1, '父级不能为空');
        }
        $collegeColumn = new BGoodsCategories();
        $info          = $collegeColumn->find_by_id($this->params['id']);
        if (empty($info)) {
            $this->fail(-1, '该记录不存在，或已被删除');
        }
        $info['image']      = trim($this->params['image']);
        $info['name']       = trim($this->params['name']);
        $info['parent_id']  = intval($this->params['parent_id']);
        $info['updated_at'] = date('Y-m-d H:i:s');
        $model              = new BGoodsCategories();
        $result             = $model->save_categories($info);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 删除分类
     */
    public function cadeleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '栏目id不能为空');
        }
        $model = new BGoodsCategories();
        $info  = $model->find_by_id($this->params['id']);
        if (empty($info)) {
            $this->fail(-1, '该记录不存在，或已被删除');
        }
        if (empty($info['parent_id'])) {// 顶级
            $child = $model->find_by_parent_id($info['id']);
            if ($child) {
                $this->fail(-1, '该栏目下面有子栏目，不能删除');
            }
        } else { // 二级
//            $count = (new Goods())->goods_count_by(['column_second_id =' . $info['value']]);
//            if ($count) {
//                $this->fail(-1, '该栏目下面有商品，不能删除');
//            }
        }
        $result = $model->findFirstById($this->params['id'])->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 巨鱼商贸商品商品分类列表
     */
    public function calistAction()
    {
        $model  = new BGoodsCategories();
        $tree   = new Tree();
        $list   = $model->goods_categories_all()->toarray();
        $mytree = $tree->array2tree($list, 'name', 'id', 'parent_id');
        $this->ok($mytree);

    }

    /**
     * 巨鱼商贸商品订单列表
     */
    public function olistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $goods = new BGoodsOrder();
        $count = $goods->order_count($this->params);
        $list  = [];
        if ($count) {
            $list = $goods->order_list($this->params);
        }

        $result = ToolsController::paging($list, $count, $this->params['limit'], $this->params['page']);

        $this->ok($result);
    }

    /**
     * 获取商品列表
     */
    public function ogoodsAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '订单id不能为空');
        }
        $model  = new BGoodsOrderDetail();
        $result = $model->order_goods_details('order_id=' . intval($this->params['id']));

        $this->ok($result);
    }

    /**
     * 取消订单
     */
    public function ocancelAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '订单id不能为空');
        }
        $model                  = new BGoodsOrder();
        $update['order_status'] = 7;
        $update['remark']       = '员工取消订单';
        $update['updated_at']   = date('Y-m-d H:i:s');

        $exist = $model->find_by_id(intval($this->params['id']), true);
        if (empty($exist)) {
            $this->fail(-1, '订单不存在');
        }
        $order = $exist->toarray();
        if (!in_array($order['order_status'], [0])) {
            $this->fail(-1, '订单不能取消');
        }
//        var_dump($order);exit;
        $exist->order_status = 7;
        $exist->remark       = '员工取消订单';
        $exist->updated_at   = date('Y-m-d H:i:s');
        $result              = $exist->save();

//        if ($result === false) {
//            $messages = $model->getMessages();
//            foreach ($messages as $message) {
//                echo $message, "\n";
//            }
//            exit;
//        }
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 发货
     */
    public function deliverAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '订单id不能为空');
        }
//        if (!isset($this->params['logistics_code']) || empty($this->params['logistics_code'])) {
//            $this->fail(-1, '物流公司编号不能为空');
//        }
//        if (!isset($this->params['orderlogistics_id']) || empty($this->params['orderlogistics_id'])) {
//            $this->fail(-1, '订单物流编号不能为空');
//        }
        $this->params['logistics_code']    = isset($this->params['logistics_code']) ? $this->params['logistics_code'] : '';
        $this->params['orderlogistics_id'] = isset($this->params['orderlogistics_id']) ? $this->params['orderlogistics_id'] : '';
        $model                             = new BGoodsOrder();
        $res                               = $model->findFirst(['id=' . intval($this->params['id'])]);
        $exist                             = $res ? $res->toarray() : [];
        if (empty($exist)) {
            $this->fail(-1, '订单不存在');
        }

        if (intval($exist['order_status']) != 1) {
            $this->fail(-1, '订单状态有误');
        }

        $res->orderlogistics_id = trim($this->params['orderlogistics_id']);
        $res->logistics_code    = trim($this->params['logistics_code']);
        $res->order_status      = 2;
        $res->updated_at        = date('Y-m-d H:i:s');

        $result = $res->save();
//        if ($result === false) {
//            $messages = $res->getMessages();
//            foreach ($messages as $message) {
//                echo $message, "\n";
//            }
//            exit;
//        }
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }

    /**
     * 巨鱼商贸商品售后列表
     */
    public function rlistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $goods = new BGoodsOrderReturn();
        $count = $goods->order_return_count($this->params);
        $list  = [];
        if ($count) {
            $list = $goods->order_return_list($this->params);
        }
        $result = ToolsController::paging($list, $count, $this->params['limit'], $this->params['page']);

        $this->ok($result);
    }

    /**
     * 巨鱼商贸商品售后列表
     */
    public function refundAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '订单id不能为空');
        }
        $model = new BGoodsOrderReturn();
        $exist = $model->order_return_details('id=' . intval($this->params['id']), '*');

        if (empty($exist)) {
            $this->fail(-1, '订单不存在');
        }
        if ($exist['status']) {
            $this->fail(-1, '订单已处理');
        }
        if ($exist['handling_way'] == 1) {
            $this->fail(-1, '该订单应该换货处理');
        }

        $order        = (new BGoodsOrder())->order_details('id=' . $exist['order_id'], '*');
        $money        = $exist['return_amount'];//退款金额
        $out_trade_no = $order['out_trade_no'];
        $subject      = '退款';
        $total        = $order['order_amount_total']; //总金额

        if ($order['pay_channel'] == 1) {
            /**
             * 支付宝
             * @see alipay::refund
             */
            $pay = $this->alipay->refund($money, $out_trade_no, $subject);
        } else {

            /**
             * 微信
             * @see xcx::refund
             */
            $pay = $this->xcx->refund($money, $out_trade_no, $subject, $total);
        }
        if (is_bool($pay) && $pay) {
            $update['status']      = 1;
            $update['handling_at'] = date('Y-m-d H:i:s');
            $result                = $model->findFirstById($this->params['id'])->update($update);
            (new BGoodsOrder())->findFirstById($exist['order_id'])->update(['order_status' => 6]);
            if ($result) {
                $this->ok([]);
            } else {
                $this->fail(-1, '操作失败');
            }
            //TODO 退佣金

        } else {
            $this->fail(-1, $pay);
        }

    }

    /**
     * 售后发货
     */
    public function rdeliverAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '订单id不能为空');
        }
        if (!isset($this->params['logistics_code']) || empty($this->params['logistics_code'])) {
            $this->fail(-1, '物流公司编号不能为空');
        }
        if (!isset($this->params['orderlogistics_id']) || empty($this->params['orderlogistics_id'])) {
            $this->fail(-1, '订单物流编号不能为空');
        }
        $model = new BGoodsOrderReturn();
        $exist = $model->order_return_details('id=' . intval($this->params['id']), '*');
        if (empty($exist)) {
            $this->fail(-1, '该售后订单不存在');
        }
        if ($exist['status']) {
            $this->fail(-1, '订单状态有误');
        }

        $update['data'] = json_encode([
            'orderlogistics_id' => trim($this->params['orderlogistics_id']),
            'logistics_code'    => trim($this->params['logistics_code']),
        ]);

        $update['status']      = 1;
        $update['handling_at'] = date('Y-m-d H:i:s');

        $result = $model->findFirstById($this->params['id'])->update($update);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '操作失败');
        }
    }


    /**
     *   邮费模板列表
     */
    public function plistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $model = new BGoodsPostage();
        $count = $model->postage_count($this->params);
        $list  = [];
        if ($count) {
            $list = $model->postage_list($this->params)->toarray();
        }
        $result = ToolsController::paging($list, $count, $this->params['limit'], $this->params['page']);

        $this->ok($result);
    }

    /**
     *   添加邮费模板
     */
    public function paddAction()
    {
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '名称不能为空');
        }
        if (!isset($this->params['postage']) || empty($this->params['postage'])) {
            $this->fail(-1, '邮费不能为空');
        }
        if (!isset($this->params['condition']) || empty($this->params['condition'])) {
            $this->fail(-1, '条件不能为空');
        }
        $data['name']       = trim($this->params['name']);
        $data['postage']    = trim($this->params['postage']);
        $data['condition']  = trim($this->params['condition']);
        $data['current']    = isset($this->params['current']) ? intval($this->params['current']) : 0;
        $data['created_at'] = date('Y-m-d H:i:s');

        $model = new BGoodsPostage();
        if ($data['current']) {
            $model->find(['current=1'])->update(['current' => 0]);
        }
        $result = $model->save_params($data);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '添加失败');
        }
    }

    /**
     *   编辑邮费模板
     */
    public function pupdateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '名称不能为空');
        }
        if (!isset($this->params['postage']) || empty($this->params['postage'])) {
            $this->fail(-1, '邮费不能为空');
        }
        if (!isset($this->params['condition']) || empty($this->params['condition'])) {
            $this->fail(-1, '条件不能为空');
        }

        $model = new BGoodsPostage();
        if ($this->params['current']) {
            $model->find(['current=1'])->update(['current' => 0]);
        }
        $exist = $model->find_by_id($this->params['id'], true);
        if (empty($exist)) {
            $this->fail(-1, '邮费模板不存在');
        }
        $exist->name       = trim($this->params['name']);
        $exist->postage    = trim($this->params['postage']);
        $exist->condition  = trim($this->params['condition']);
        $exist->current    = intval($this->params['current']);
        $exist->updated_at = date('Y-m-d H:i:s');
        $result            = $exist->save();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail('修改失败');
        }
    }

    /**
     *   删除邮费模板
     */
    public function pdeleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $id    = trim($this->params['id']);
        $model = new BGoodsPostage();
        $exist = $model->find_by_id($this->params['id']);
        if (empty($exist)) {
            $this->fail(-1, '邮费模板不存在');
        }
        if ($exist['current']) {
            $this->fail(-1, '当前邮费模板不能删除');
        }
        $result = $model->find(['id In (' . $id . ')'])->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '删除失败');
        }
    }


    /**
     *   轮播图列表
     */
    public function slistAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $this->params['page'] = 1;
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $this->params['limit'] = 10;
        }
        $model = new BGoodsSlideShow();
        $count = $model->slideshow_count($this->params);
        $list  = [];
        if ($count) {
            $list = $model->slideshow_list($this->params)->toarray();
        }
        $result = ToolsController::paging($list, $count, $this->params['limit'], $this->params['page']);

        $this->ok($result);
    }

    /**
     *   添加轮播图
     */
    public function saddAction()
    {
        if (!isset($this->params['image']) || empty($this->params['image'])) {
            $this->fail(-1, '图片不能为空');
        }
        if (!isset($this->params['url']) || empty($this->params['url'])) {
            $this->fail(-1, 'url不能为空');
        }
        $data['image']      = trim($this->params['image']);
        $data['url']        = trim($this->params['url']);
        $data['status']     = isset($this->params['status']) ? intval($this->params['status']) : 0;
        $data['created_at'] = date('Y-m-d H:i:s');

        $model  = new BGoodsSlideShow();
        $result = $model->save_params($data);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '添加失败');
        }
    }

    /**
     *   编辑轮播图
     */
    public function supdateAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        if (!isset($this->params['image']) || empty($this->params['image'])) {
            $this->fail(-1, '图片不能为空');
        }
        if (!isset($this->params['url']) || empty($this->params['url'])) {
            $this->fail(-1, 'url不能为空');
        }

        $model = new BGoodsSlideShow();
        $exist = $model->find_by_id($this->params['id'], true);
        if (empty($exist)) {
            $this->fail(-1, '轮播图不存在');
        }
        $exist->image      = trim($this->params['image']);
        $exist->url        = trim($this->params['url']);
        $exist->status     = intval($this->params['status']);
        $exist->updated_at = date('Y-m-d H:i:s');
        $result            = $exist->save();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail('修改失败');
        }
    }

    /**
     *   删除轮播图
     */
    public function sdeleteAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, 'id不能为空');
        }
        $id    = trim($this->params['id']);
        $model = new BGoodsSlideShow();
        $exist = $model->find_by_id($this->params['id']);
        if (empty($exist)) {
            $this->fail(-1, '轮播图不存在');
        }
        $result = $model->find(['id In (' . $id . ')'])->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '删除失败');
        }
    }

}
